<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
	<div id="app" class="wrapper wrapper-content ">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-body">
                        <input type="hidden" id="isEdit" th:value="${"$"}{isEdit} ? 'Y' : 'N'">
						<form class="form-horizontal m-t" id="${entityLowerName}Form"
                              th:action="${"$"}{isEdit} ? @{/${cfg.module}/${entityLowerName}/edit} : @{/${cfg.module}/${entityLowerName}/add}">
                            <input type="hidden" name="${pkColName}" th:value="${"$"}{${entityLowerName}.${pkColName}}">
                            <#assign isSearch="N">
                            <#include "field.html.ftl"/>

							<div class="form-group">
								<div class="col-sm-8 col-sm-offset-2">
                                    <button type="button" class="btn btn-primary" onclick="save()">
                                        <i class="fa fa-save" aria-hidden="true"></i> 保存
                                    </button>
                                    <a th:href="@{/${cfg.module}/${entityLowerName}/gotoSearch}" class="btn btn-success">
                                        <i class="fa fa-arrow-left" aria-hidden="true"></i> 返回
                                    </a>
								</div>

							</div>
						</form>
					</div>
				</div>
			</div>
	    </div>
	</div>
	<div th:include="include::footer"></div>
    <script th:inline="javascript"> var ctx = [[@{/}]] ; </script>
    <script type="text/javascript" th:src="@{/js/appjs/app-common.js}"></script>
	<script type="text/javascript">
        $(document).ready(function () {
            validateRule();
        });

        function save(){
            var frm = $("#${entityLowerName}Form");
            if(!frm.valid()){
                return;
            }

            $.ajax({
                type : "POST",
                url : frm.attr("action"),
                data : frm.serialize(),
                async : true,
                error : function(request) {
                    layer.alert("连不上服务器");
                },
                success : function(res){
                    if(res.status == 200){
                        layer.msg(res.msg);
                        if("N" == $('#isEdit').val()){
                            frm.get(0).reset();
                        }
                    }else{
                        layer.alert(res.msg, {icon: 5});
                    }
                }
            });
        }

        function validateRule(){
            var icon = "<i class='fa fa-times-circle'></i> ";
        <#if hasDecimalField>
            addAmountValidator();
        </#if>
            $("#${entityLowerName}Form").validate({
                rules: {
        <#list editFields as field>
                    ${field.propertyName}: {
                        required: true<#if field.propertyType=="BigDecimal" || field.propertyType == "Integer" || field.propertyType == "Long">,</#if>
                <#if field.propertyType=="BigDecimal">
                        number: true,
                        amountValidator: true
                <#elseif field.propertyType == "Integer" || field.propertyType == "Long">
                        digits: true,
                        amountValidator: true
                </#if>
                    }<#if field_has_next>,</#if>
        </#list>
                },
                messages: {
        <#list editFields as field>
                    ${field.propertyName}: {
                        required: icon + "请输入${field.comment}"<#if field.propertyType=="BigDecimal" || field.propertyType == "Integer" || field.propertyType == "Long">,</#if>
                <#if field.propertyType=="BigDecimal">
                        number: icon + "请输入数字"
                <#elseif field.propertyType == "Integer" || field.propertyType == "Long">
                        digits: icon + "请输入整数"
                </#if>
                    }<#if field_has_next>,</#if>
        </#list>

                }
            })
        }
	</script>
</body>
</html>
